let Util = {
  getUserToken: getUserToken,
  sendShareMessage: sendShareMessage,
  download: download,
  wxUrl:wxUrl,
  parseURL:parseURL
};
let filters = {
  parseURL:function(url) {
    let a = document.createElement('a');
    a.href = url;
    return {
      params: (function (name) {
        console.log(url.indexOf('?'))
          let ret = {},
            param = a.href.split('?')[1],
            seg = param.split('&'),
            len = seg.length,
            i = 0,
            s;
          for (; i < len; i++) {
            if (!seg[i]) {
              continue;
            }
            s = seg[i].split('=');
            ret[s[0]] = s[1];
          }
          return ret;
      })()
    }
  }
};
const url = window.location.href;
if(url.indexOf('wx_app')>0){
  var  wx_app = filters.parseURL(url).params.wx_app||'';
}
function getUserToken() {
  // let token = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzZWNhciIsImFwcGtleSI6IjEzMjYyNzcwMjkxIiwic2VjcmV0IjoiZjA2NzY2MDAwYzFhNGEwNGY0ZDVkOGFiODFmMTEyZWYifQ.08cygSS4jR6OZA82G1z7VvPpsBlUXiBz8TiVl0xCGV0"
  let token = "";
  let userAgentObj = navigator.userAgent;
  //小程序兼容
  return new Promise(function(resolve) {
    if(wx_app){//小程序
      if(url.indexOf('token')>0){
        let token = filters.parseURL(url).params.token;
        resolve(token);
      }
    }else{//app
        if(userAgentObj.match(/Android/i)){
          if(window.sessionStorage.getItem('token')){
            token = window.sessionStorage.getItem('token')
          }else{
            token = window.control.getToken();
            window.sessionStorage.setItem('token',token);
          }
          resolve(token)
        }else if(userAgentObj.match(/iPhone/i)){
          // setTimeout(() => {
            if(window.sessionStorage.getItem('token'))
            {
              token = window.sessionStorage.getItem('token')
            }else{
              token = getToken();
              window.sessionStorage.setItem('token',token)
            }
            resolve(token)

          // }, 400)
        }
    }

  }).catch(function(err){
    console.log(err);
  });

}

function sendShareMessage(title,desc,url,surl){
  if(!wx_app){
    let userAgentObj = navigator.userAgent;
    if(userAgentObj.match(/Android/i)){
      window.control.getAdInfo(title,desc,url,surl);
    }else if(userAgentObj.match(/iPhone/i)) {
      setTimeout(() => {
        getAdInfo(title, desc, url, surl);
      }, 10)
    }
  }
}
//环境判断
function download(token){
  let userAgentObj = navigator.userAgent;
  if(userAgentObj.match(/MicroMessenger/i)){//微信判断
    if(userAgentObj.match(/Android/i)){
      window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919'
    }else if(userAgentObj.match(/iPhone/i)) {
        window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
    }else{
      window.location.href="http://download.xingjiakeji.com/";
    }
    return false;
  }else{
    if(!token || token == undefined || token == ""){
        if(userAgentObj.match(/Android/i)){
          window.location.href = 'http://app.qq.com/#id=detail&appid=1106220919';
        }else if(userAgentObj.match(/iPhone/i)) {
          window.location.href = "https://itunes.apple.com/cn/app/%E7%94%B5%E6%96%91%E9%A9%AC-%E8%AE%A9%E5%87%BA%E8%A1%8C%E6%9B%B4%E5%AE%89%E5%85%A8/id1252928655?mt=8";
        }
      return false;
    }
  }
}
//小程序url
function wxUrl(url){
  var length =(process.env.MAIN_ROOT).length;
  // 29
  var wx_url = url.substr(length),
    seg = wx_url.split('&'),
    newUrl = '',
    s;
  for (var i=0; i < seg.length; i++) {
    s = seg[i].split('=');
    console.log('web'+[i]+','+s)
    if(s[0] =='share'|| s[0]=='token'){
      if(s[0]=='share'){

      }else{
        newUrl+='&token=';
      }
    }else{
      if(i== seg.length-2){
        newUrl += seg[i];
      }else{
        newUrl += seg[i]+'&';
      }
      // console.log(newUrl);
    }
  }
  return  newUrl;
}
//获取连接参数
function parseURL(url) {
  var a = document.createElement('a');
  a.href = url;
  return {
    params: (function () {
      if(a.href.indexOf('?')>0){
        var ret = {},
        param = a.href.split('?')[1],
        seg = param.split('&'),
        len = seg.length,
        i = 0,
        s;
        for (; i < len; i++) {
          if (!seg[i]) {
            continue;
          }
          s = seg[i].split('=');
          ret[s[0]] = s[1];
        }
        return ret;
      }else{
        console.log('链接无参数:'+url)
        return {};
      }
    })()
  }
}

export default Util;
